<?php

namespace app\home\controller;

use controller\BasicWechat;
use app\comm\service\MyYcardService;
use app\comm\service\UserOrderService;
use app\comm\service\UserService;
use app\comm\service\GoodsService;
use think\Controller;
use think\Db;
use app\comm\controller\PcLogController;
use ServS\DbConf;
use app\index\service\OrderService;

//订单类控制器
class Order extends PcLogController {

	/**
	 *订单首页
	 * @return [type] [description]
	 */
	public function index(){
		return view('order/index');	
	}

	/**
	 * 我的订单
	 */
	public function orderlistapi(){
		$page =request()->get("page",0);
		$limit =request()->get("limit",10);
		$status =request()->get("status");
		$uid  = $this->userinfo['uid'];
		$map["uid"] = $uid;
		if($status!="-1"){
			$map["order_status"] = $status;
		}
		$dataorder = Db::table("ys_order")->where($map)->page($page,$limit)->order("addtime desc")->select();
		foreach ($dataorder as $key => &$value) {
			$orderdetaile = Db::table("ys_order_details")->where("order_id",$value["id"])->select();
			foreach ($orderdetaile as $key => &$valu) {
				$valu["goods"] = Db::table("ys_goods")->where("id",$valu["goods_id"])->find();
				$valu["goods"]["description"] = htmlspecialchars_decode($valu["goods"]["description"]);
				$valu["goods"]["detail"] = htmlspecialchars_decode($valu["goods"]["detail"]);
			}
			$value["orderdetail"] =$orderdetaile;
		}
		return json(["status"=>1,"data"=>$dataorder]);	
	}

	/*
	 *订单详情 每个订单可能有好几个商品
	 */
	public function orderdetail(){
		$id = request()->get("id");	
		$order = Db::table("ys_order")->where("id",$id)->find();
		$orderdetaile = Db::table("ys_order_details")->where("order_id",$id)->select();
		foreach ($orderdetaile as $key => &$value) {
			$value["goods"] = Db::table("ys_goods")->where("id",$value["goods_id"])->find();
			$value["goods"]["description"] = htmlspecialchars_decode($value["goods"]["description"]);
			$value["goods"]["detail"] = htmlspecialchars_decode($value["goods"]["detail"]);
		}
		$this->assign('order',$order);
		$this->assign('orderdetaile',$orderdetaile);
		$this->assign('id',$id);
		return view('order/orderdetail');	
	}

	/**
	 *订单详情api
	 */
	public function orderdetailapi(){
		$id = request()->get("id");
		$order = Db::table("ys_order")->where("id",$id)->find();
		$orderdetaile = Db::table("ys_order_details")->where("order_id",$id)->select();
		foreach ($orderdetaile as $key => &$value) {
			$value["goods"] = Db::table("ys_goods")->where("id",$value["goods_id"])->find();
			$value["goods"]["description"] = htmlspecialchars_decode($value["goods"]["description"]);
			$value["goods"]["detail"] = htmlspecialchars_decode($value["goods"]["detail"]);
		}
		return json(["status"=>1,"order"=>$order,"orderdetaile"=>$orderdetaile]);
	}

	/**
	 * 获取购物车的列表
	 */
	public function getordercar(){
		$ordecar 	=  Db::table("ys_order_cart")->where("uid",$this->userinfo["uid"])->select();	
		foreach ($ordecar as $key => &$value) {
				$value["goods"]	  			= Db::table("ys_goods")
				                               ->where("id",$value["goods_id"])->find();
				$value["goods_spec_detail"]	 = Db::table("ys_goods_spec_detail")
				                               ->where("id",$value["specid"])->find();
				$value["store_goods_stock"]	 = Db::table("ys_store_goods_stock")
				                               ->where("id",$value["store_goods_stock_id"])->find();
				$value["store"]    			 = Db::table("ys_store")
												->where("id",$value["storeid"])->find();	                            
		}
		$this->assign('ordecar',$ordecar);
		return view('order/getordercar');	
	}

	/**
	 * 删除购物车
	 */
	public function deletecar(){
		$id = request()->get("id");
		$rest = Db::table("ys_order_cart")->where("id",$id)->delete();
		$this->redirect(url('order/getordercar'),1,'删除成功');
	}

	/**
	 * 用户评价 post
	 * ajax提交
	 */
	public function yonghusave(){
		$goods = new GoodsService();
		$post  = $this->request -> post();
		$goods -> addgoodscomm($post);
		$this -> success('恭喜, 数据保存成功!', '');	
	}

	/**
	 * 用户收藏商品 ajax收藏
	 */
	public function collegood(){
		$id = request()->get("id");
		$data['uid']  	   = $this->userinfo['uid'];
		$data['goods_id']  = $id;
		$data['store_id']  = request()->get("store_id");
		$userservice  	   = new UserService;
		$rest = $userservice->usercollectgoods($data);
		if($rest==5){
			return json(['status'=>5,'该商品已经收藏过']);	
		}
		if($rest==0){
			return json(['status'=>0,'网络错误，收藏失败']);	
		}
		return json(['status'=>1,"data"=>'成功收藏']);	
	}

	/**
	 * 加入购物车
	 */
	public function addusecar(){
		$goods_id = request()->get("goods_id");
		$sum = request()->get("sum");
		$storeid = request()->get("storeid");
		$goods_price = request()->get("goods_price");
		$goods_spec_detail_id = request()->get("goods_spec_detail_id");
		$store_goods_stock_id = request()->get("store_goods_stock_id");
		$data = ["uid" =>$this->userinfo['uid'],
				 'goods_id' =>$goods_id ,
				 "specid"	=>$goods_spec_detail_id,
				 "storeid"	=>$storeid,
				 "goods_price"=>$goods_price,
				 "store_goods_stock_id"=>$store_goods_stock_id
				];
		$map["uid"]		 = ["eq",$this->userinfo['uid']];
		$map["goods_id"] = $goods_id ;
		$map["specid"]	 = ["eq",$goods_spec_detail_id];
		$map["storeid"]		 = ["eq",$storeid];
		$ordecar = Db::table("ys_order_cart")->where($map)->find();
		//return $ordecar;
		if(!empty($ordecar)){
			$sumprice = $sum + $ordecar["goods_sum"] ;
			$data = ["goods_sum_price"=>$sumprice*$goods_price,"goods_sum"=>$sumprice];
			Db::table("ys_order_cart")->where($map)->update($data);
		}else{
			$data["addtime"]= time();
			$data["goods_sum"] =$sum;
			$data["goods_sum_price"]=$sum * $goods_price;

			Db::table("ys_order_cart")->insert($data);
		}
		return json(['status'=>1,'msg'=>"保存成功"]);
	}

	/**
	 * 生成订单前一个页面
	 */
	public function preindex(){
		$goods_id = request()->get("goods_id");
		$sum = request()->get("sum");
		$storeid = request()->get("storeid");
		$goods_price = request()->get("goods_price");
		$goods_spec_detail_id = request()->get("goods_spec_detail_id");
		$store_goods_stock_id = request()->get("store_goods_stock_id");

		$data = request()->get();
		$data["sum_price"]	=$sum * $goods_price;
		$data["uid"]        =$this->userinfo["uid"];
		$this->assign('data',$data);

		$goods = Db::table("ys_goods")->where("id",$goods_id)->find();
		$this->assign('goods',$goods);
		$goods_spec_detail = Db::table("ys_goods_spec_detail")->where("id",$goods_spec_detail_id)->find();
		$this->assign('goods_spec_detail',$goods_spec_detail);
		$store_goods_stock = Db::table("ys_store_goods_stock")->where("id",$store_goods_stock_id)->find();
		$this->assign('store_goods_stock',$store_goods_stock);
		//dd($goods);dd($goods_spec_detail);dd($store_goods_stock );
		return view('order/preindex');
     
	}

	/**
	 * 提交订单个商品生成订单
	 */
	public function addorder(){
		$OrderService = new OrderService();
		$post  = $this->request -> post();
		$result = $OrderService -> addorder($post);
		//生成订单到自己的支付页面
		if($result){
			return json(["status"=>1,"data"=>$result]);	
		}
		return json(["status"=>0,"data"=>'']);		
	}

	/**
	 * 提交订单 购物车生成订单
	 */
	public function addorderbycar(){
		$orderService =  new OrderService();
		$post         =  $this->request -> post();
		$post["uid"]  =  $this->userinfo['uid'];
		$result       =  $orderService->addorderbycar($post);
		if($result){
			return json(["status"=>1,"data"=>$result]);
		}
		return json(["status"=>0,"data"=>'']);
	}

	/**
	 * 支付页面展示商品
	 * @return [type] [description]
	 */
	public function orderpay(){
		$id = request()->get("id");	
		$order = Db::table("ys_order")->where("id",$id)->find();
		$orderdetaile = Db::table("ys_order_details")->where("order_id",$id)->select();
		foreach ($orderdetaile as $key => &$value) {
			$value["goods"] = Db::table("ys_goods")->where("id",$value["goods_id"])->find();
			$value["goods"]["description"] = htmlspecialchars_decode($value["goods"]["description"]);
			$value["goods"]["detail"] = htmlspecialchars_decode($value["goods"]["detail"]);
		}
		$this->assign('order',$order);
		$this->assign('orderdetaile',$orderdetaile);

		$paytype =  Db::table("ys_dic_config")->where("name","wallet")->select();
		$this->assign('paytype',$paytype);
		//return json(['orderdetaile'=>$orderdetaile]);
		return view('order/orderpay'); 
	}

	/**
	 *商品订单评论
	 */
	public function pinglun(){
		$uid   			= $this->userinfo['uid'];
		$postData       = file_get_contents('php://input', true);
		$post 	        = json_decode($postData,true);
		if(1>3){
			return json(['status'=>0,"msg"=>"已经评论过"]);
		}	
		 $commentdata       = [
			"goods_id"  	=> 	$post["goodsid"],	//商品id
			"content" 		=>  $post["msg"],		//评论内容
			"uid" 			=> 	$uid,
			"is_show" 		=>  '', 			 	//是否显示 默认为1显示
			"images" 		=> 	'',	
			"order_id" 		=> 	$post["order_id"],
			"is_anonymous" 	=> 	0,	 				//是否匿名评价:0不是，1是
			"is_deleted" 	=> 	0,
			"store_id" 		=> 	$post["store_id"],
			"addtime" 		=>time()
		  ];
		$resrt        = Db::table('ys_goods_comment')->insert($commentdata);
		return json(['status'=>1,"msg"=>"评论完成"]);
	}

	/**
	 * 订单品论
	 */
	public function ordercommon(){
		$id = request()->get("id");
		$this->assign('id',$id);
		return $this->fetch("order/ordercommon");
	}

	/**
	 * 订单评论post
	 */
	public function postcomm(){
		$userservice = new UserOrderService;
		$postData  	 = file_get_contents('php://input', true);
		$obj 	     =json_decode($postData,true);
		$restul  	 = $userservice ->commitorder($obj);
		return json($restul);
	}
	/**
	 * 退款dingdan 
	 */
	public function goodback(){
		$id = request()->get("id");
		// $userorderservice = new UserOrderService();
		// $goodsservice     =  new GoodsService;
		// $order = $userorderservice   ->orderdetaile($id);
		// //return json($order);
		$this->assign('id',$id);
		return $this->fetch("order/goodback");
	}
	/**
	 * 退款api
	 */
	public function goodbackapi(){
		$id = request()->get("id");
		$userorderservice = new UserOrderService();
		$goodsservice     =  new GoodsService;
		$order = $userorderservice   ->orderdetaile($id);
		return json($order);
		$this->assign('order',$order);
		return $this->fetch("order/goodback");
	}
	
	/**
	 * 退款订单详情
	 */
	public function goodbackorderdertail(){
		$id = request()->get("id");
		// $userorderservice = new UserOrderService();
		// $ordrdtail = $userorderservice ->ordtailebyidtuikuan($id);
		$this->assign('id',$id);
		return $this->fetch("order/goodbackorderdertail");
	}

	/**
	 * 退款订单详情
	 */
	public function goodbackorderdertailapi(){
		$id = request()->get("id");
		$userorderservice = new UserOrderService();
		$ordrdtail = $userorderservice ->ordtailebyidtuikuan($id);
		return json($ordrdtail);
	}

	/**
	 *退款原因
	 */
	public function fadeback(){
		$postData = file_get_contents('php://input', true);
		$obj=json_decode($postData,true);
		$renstru = Db::table("ys_order_details")->find($obj['id']);
		if($renstru["is_tuihuo"]=="1"){
			return json(['status'=>0,"msg"=>"不能重复提交退款"]);
		}
		$userorderservice = new UserOrderService();
		$userorderservice ->ordertailback($obj);
		return json(['status'=>1,"msg"=>"成功提交退款"]);
	}

	/**
	 * 取消退款
	 */
	public function caclfeed(){
		$id = request()->get("id");
		$renstru = Db::table("ys_order_details")->where("id",$id)->update(["is_tuihuo"=>0]);
		return $renstru;
	}
}